package com.jrx.anytxn.reporting.job.xib.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.BtBatchStatus;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.ext.ExtBtBatchStatusMapper;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.mapper.ext.ExtCmInteCoLonRepayPlanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @comment: 贷款还款计划Listener
 * @author: hsoong
 * @date: 2021/7/2 09:24
 */
@Service
public class RepayPlanReportingListener implements JobExecutionListener {

    private static Logger logger = LoggerFactory.getLogger(RepayPlanReportingListener.class);

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Resource
    private ExtBtBatchStatusMapper extBtBatchStatusMapper;

    @Resource
    private ExtCmInteCoLonRepayPlanMapper extCmInteCoLonRepayPlanMapper;


    @Override
    public void beforeJob(JobExecution jobExecution) {
        logger.info("任务执行前...");

        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        BtBatchStatus statuses = extBtBatchStatusMapper.selectByStatusAndJobName(ReportingConstant.BATCH_SATTUS_2, ReportingConstant.CUTOVER_END, org.getTenantId(), org.getToday());
        if (statuses != null) {
            String todayStr = DateUtils.format(org.getToday(), "yyyy-MM-dd");
            final Date today = DateUtils.parseDate(todayStr, "yyyy-MM-dd");
            logger.info("尝试清空指定日期（{}）的CmInteCoLonCrdtInfo映射表表数据", todayStr);
            extCmInteCoLonRepayPlanMapper.deleteByEtlDt(today);
            logger.info("映射表数据指定日期（{}）清理完成", todayStr);
        }
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        logger.info("任务执行结束");
    }
}
